package artid.com.arttogo.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.ArtListDetailAdapter;
import artid.com.arttogo.bean.ArtistFollow;
import artid.com.arttogo.bean.GoodsInfo;
import artid.com.arttogo.bean.LikeGoods;
import artid.com.arttogo.bean.ShareItem;
import artid.com.arttogo.bean.ShowPhoto;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.fragment.BaseFragment;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.ToastUtil;
import artid.com.arttogo.view.CircleImageView;
import artid.com.arttogo.view.MyArtGoodAdGallery;
import artid.com.arttogo.view.MyListView;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableScrollView;

/**
 * 艺术商品详情
 *
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class MallDetailsActivity extends BaseFragment {
    private Context mContext;
    private ImageLoader mImageLoader;
    ShowPhoto photoItem = new ShowPhoto();
    MyListView mlist;
    String id = "";
    ImageView mImgShare;
    PullToRefreshLayout mPullLayout;
    int pageIndex = 1, pagesize = 10;
    PullableScrollView mscroll;
    View hideView;
    ShareItem shareItem;
    String srcImg = "";
    String uid = "";
    ArtListDetailAdapter adapter;
    String messid = "";
    Boolean isReplyPerson = false;
    String commentId = "";
    private int width;
    String money = "";

    private static List<ImageView> dotViewList;
    private MyArtGoodAdGallery mMyviewPager;
    private TextView mTextArtistName;
    private TextView mTextArtName;
    private TextView mTextArtistNum;
    private TextView mTextBuy;//立即购买
    private ImageView mMall_det_contact;
    private View mView;
    TextView mMalldet_artistname;//艺术家名字
    TextView mMalldet_viewnum;
    TextView mMall_det_info;//类别尺寸年代
    TextView indexSum, indexCur;
    ImageView  viewFollow;
    View mCol_down_half;
    ImageView mMall_det_likeimg;
    TextView mTextLikeNum;//喜欢的数量
    CircleImageView imgHead;
    TextView mTextPrice;
    View mBtnBack;//返回键
    TextView mTextUploadTime;//上传时间
    ImageView mImgIsRecord;//已备案
    View mViewLikeLayout;//点赞的布局，用于增大触摸面积
    View mViewShare;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mContext = getActivity();
        RequestQueue mQueue = Volley.newRequestQueue(mContext);
        mImageLoader = new ImageLoader(mQueue, new BitmapCache());
        mView = (View) inflater.inflate(R.layout.activity_mall_details, null);
        viewFollow = (ImageView) mView.findViewById(R.id.mall_det_view_follow);
        indexSum = (TextView) mView.findViewById(R.id.index_sum);
        indexCur = (TextView) mView.findViewById(R.id.index_cur);
        mMalldet_artistname = (TextView)mView.findViewById(R.id.mall_det_artist) ;
        mMyviewPager = (MyArtGoodAdGallery) mView
                .findViewById(R.id.myviewPager);
        mMyviewPager.setmPager(MallDetailPagerActivity.mViewPager);
        mMyviewPager.setIndexTextView(indexCur);
        mTextUploadTime = (TextView)mView.findViewById(R.id.mall_det_upload);
        mCol_down_half = (LinearLayout) mView.findViewById(R.id.col_down_half);
        mTextArtistName = (TextView) mView
                .findViewById(R.id.mall_det_art_name);
        mImgIsRecord = (ImageView)mView.findViewById(R.id.lot_det_isrecord) ;
        mTextArtName = (TextView) mView.findViewById(R.id.mall_det_name);
        mTextArtistNum = (TextView) mView
                .findViewById(R.id.mall_det_artist_num);
        mViewLikeLayout = (View)mView.findViewById(R.id.mall_det_like_layout);
        mTextBuy = (TextView) mView.findViewById(R.id.mall_det_buy);
        mTextBuy.setOnClickListener(buyClick(null));
        mMall_det_contact = (ImageView)mView.findViewById(R.id.mall_det_contact);
        mMall_det_info = (TextView)mView.findViewById(R.id.mall_det_cate_size_year);
        mMall_det_likeimg = (ImageView)mView.findViewById(R.id.mall_det_like);
        mTextLikeNum = (TextView)mView.findViewById(R.id.tv_like_sum);
        mTextPrice = (TextView)mView.findViewById(R.id.mall_det_price);
        mMalldet_viewnum = (TextView)mView.findViewById(R.id.mall_det_lot);
        imgHead = (CircleImageView)mView.findViewById(R.id.mall_det_headicon);
        mViewShare = (View)mView.findViewById(R.id.mall_det_share);

        mBtnBack = (View)mView.findViewById(R.id.LotDetailFragment_HideTitle_backBtn);
        mBtnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        return mView;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        id = getArguments().getString("id");
        reqData(id);

    }

    View.OnClickListener buyClick(final GoodsInfo item) {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (item == null) {
                    ToastUtil.toast(getActivity(), "数据请求中");
                    return;
                }
                if (AppUtil.LoginVer(mContext)) {
                    return;
                }
                Intent intent = new Intent(mContext, EnsureOrderActivity.class);
                intent.putExtra("id", item.getGoods_id());
                startActivity(intent);
            }
        };
    }

    void reqData(String goodsid) {
        VolleyBasic.getInstanceNoDialog((Activity) mContext).post(
                Cache.goodsInfo, VolleyMap.getInstance().goodsInfo(goodsid),
                GoodsInfo.class, new VolleyCallback() {
                    @SuppressLint("ResourceAsColor")
                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        final GoodsInfo item = (GoodsInfo) vd.getData();
                        if (item == null) {
                            return;
                        }
                        if (vd.getCode() == -99) {
                            AppUtil.LoginVer(mContext);
                            return;
                        }
                        mViewShare.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //TOdo 待添加分享链接
                                AppUtil.showShare(mContext,item.getGoods_name(),item.getGoods_name()+"-"+item.getRealname(),item.getGoods_image()[0],"");
                            }
                        });
                        mTextArtistName.setText(item.getGoods_name());
                        mMalldet_artistname.setText(item.getRealname()+"作品");
                        mMalldet_viewnum.setText(item.getGoods_click());
                        mMall_det_info.setText(item.getGc_name()+"/"+item.getGoods_size()+"/"+item.getGoods_age());
                        if (item.getIs_auth().equals("1")) {
                           mImgIsRecord.setVisibility(View.VISIBLE);
                        }else {
                            mImgIsRecord.setVisibility(View.GONE);
                        }


                        if (item.getIs_like().equals("1")) {
                            mMall_det_likeimg
                                    .setImageResource(R.drawable.icon_like_sel);
                        } else {
                            mMall_det_likeimg
                                    .setImageResource(R.drawable.ppxq_ico_dz);
                        }
                        mViewLikeLayout
                                .setOnClickListener(new OnClickListener() {

                                    @Override
                                    public void onClick(View arg0) {
                                        // TODO Auto-generated method stub
                                        if (item.getIs_like().equals("1")) {
                                            reqisLike(item.getGoods_id(), false);
                                        } else {
                                            reqisLike(item.getGoods_id(), true);
                                        }

                                    }
                                });
                        mTextLikeNum.setText(item.getGoods_commend_num());

                        if (item.getArtistimg().contains("http")) {
                            ImageListener listener = ImageLoader
                                    .getImageListener(imgHead,
                                            R.drawable.ic_menu_rotate,
                                            R.drawable.ic_menu_rotate);

                            mImageLoader.get(item.getArtistimg(), listener);


                        }
                        mTextLikeNum
                                .setOnClickListener(new OnClickListener() {

                                    @Override
                                    public void onClick(View arg0) {
                                        // TODO Auto-generated method stub
                                        Intent intent = new Intent(
                                                getActivity(),
                                                CollecterActivity.class);
                                        intent.putExtra("id",
                                                item.getArtist_id());
                                        startActivity(intent);
                                    }
                                });
                        mTextArtName.setText(item.getStore_name());
                        mTextArtistNum.setText(item.getArtist_goods_num()
                                + "件作品");
                        mTextUploadTime.setText(item.getGoods_addtime()+"上传");

                        if (item.getIs_artist_follow().equals("1")) {
//                            mMall_det_follow.setText("已关注");
//                            mMall_det_follow.setTextColor(Color.WHITE);
                            viewFollow
                                    .setImageResource(R.drawable.ysj_btn_guanzhu_sel);
                        } else {
//                            mMall_det_follow.setText("关注");
//                            mMall_det_follow
//                                    .setTextColor(Color.parseColor("#f7823b"));
                            viewFollow
                                    .setImageResource(R.drawable.ysj_btn_guanzhu);
                        }
                        viewFollow.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub

                                if (item.getIs_artist_follow().equals("1")) {
                                    reqFollow(item.getArtist_id(), false);
                                } else {
                                    reqFollow(item.getArtist_id(), true);
                                }
                            }
                        });
                        //是否验证通过
//                        if (item.getIs_auth().equals("1")) {
//                            mMall_det_view_rec.setVisibility(View.VISIBLE);
//                            mMall_det_view_certif.setVisibility(View.VISIBLE);
//                        } else {
//                            mMall_det_view_rec.setVisibility(View.GONE);
//                            mMall_det_view_certif.setVisibility(View.GONE);
//                        }
                        if (item.getGoods_storage().equals("0")) {
                            // mMall_det_buy.setText();
                            mTextPrice.setText("已售出");

                            mTextBuy.setTextColor(Color
                                    .parseColor("#c9c9c9"));
                            mTextBuy.setBackgroundColor(Color
                                    .parseColor("#dedede"));
                            mTextBuy.setClickable(false);
                        } else {
                            mTextPrice.setText("¥"+item.getGoods_price());
                            mTextBuy.setClickable(true);
                            mTextBuy.setOnClickListener(buyClick(item));
                        }

                        mMall_det_contact
                                .setOnClickListener(new OnClickListener() {

                                    @Override
                                    public void onClick(View arg0) {
                                        // TODO Auto-generated method stub
                                        AppUtil.testChat(mContext,item.getUid(), item.getArtistimg(), item.getGoods_name(), item.getGoods_image()[0]);
                                    }
                                });

                        //
                        if (item.getGoods_image() != null) {
                            try {
                                mMyviewPager.start(getActivity(), item.getGoods_image(),
                                        null, 3000);
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                            List<ShowPhoto> listPhotos = new ArrayList<ShowPhoto>();
                            for (int i = 0; i < item.getGoods_image().length; i++) {
                                ShowPhoto showitem = new ShowPhoto();
                                String url = item.getGoods_image()[i];
                                showitem.setUrl(url);
                                showitem.setName(item.getGoods_name());
                                listPhotos.add(showitem);
                            }
                            photoItem.setList(listPhotos);
                            mMyviewPager
                                    .setMyOnItemClickListener(new MyArtGoodAdGallery.MyOnItemClickListener() {

                                        @Override
                                        public void onItemClick(int curIndex) {
                                            // TODO Auto-generated method stub
                                            Intent intent = new Intent(
                                                    mContext,
                                                    PhotoActivity.class);
                                            intent.putExtra("item", photoItem);
                                            intent.putExtra("image_index",
                                                    curIndex);
                                            startActivity(intent);
                                        }
                                    });
                        }
                        try {
                            indexSum.setText(String.valueOf(item
                                    .getGoods_image().length));
                        } catch (Exception e) {
                            // TODO: handle exception
                            indexSum.setText("1");
                        }

                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    void reqFollow(String id, final Boolean setFollow) {
        String state = "";
        if (setFollow) {
            state = "1";
        } else {
            state = "2";
        }
        VolleyBasic.getInstanceNoDialog((Activity) mContext).post(
                Cache.artistFollow,
                VolleyMap.getInstance().artistFollow(
                        AppUtil.getUser().getToken(), id, state),
                ArtistFollow.class, new VolleyCallback() {

                    @SuppressLint("ResourceAsColor")
                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        ArtistFollow itemArtistFollow = (ArtistFollow) vd
                                .getData();

                        if (itemArtistFollow.getState().equals("1")) {
                            if (setFollow) {
                                viewFollow
                                        .setImageResource(R.drawable.ysj_btn_guanzhu_sel);
                            } else {
                                viewFollow
                                        .setImageResource(R.drawable.ysj_btn_guanzhu);
                            }
                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    String itemstate = "";

    void reqisLike(final String id, final Boolean isLike) {
        if (AppUtil.LoginVer(mContext)) {
            return;
        }
        if (isLike) {
            itemstate = "1";
        } else {
            itemstate = "0";
        }
        VolleyBasic.getInstance((Activity) mContext).post(Cache.likeGoods,
                VolleyMap.getInstance().likeGoods(id, itemstate),
                LikeGoods.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        LikeGoods itemArtShownum = (LikeGoods) vd.getData();
                        if (vd.getCode() == 1) {
                            try {
                                String num = "";
                                String head = AppUtil.getUser().getHeadurl();
                                if (isLike) {
                                    num = String.valueOf(Integer
                                            .valueOf(mTextLikeNum
                                                    .getText().toString()) + 1);
                                    mMall_det_likeimg
                                            .setImageResource(R.drawable.icon_like_sel);


                                } else {
                                    num = String.valueOf(Integer
                                            .valueOf(mTextLikeNum
                                                    .getText().toString()) - 1);
                                    mMall_det_likeimg
                                            .setImageResource(R.drawable.ppxq_ico_dz);

                                }
                               // setHeadIcon(listIcon);
                                mViewLikeLayout
                                        .setOnClickListener(new OnClickListener() {

                                            @Override
                                            public void onClick(View arg0) {
                                                // TODO Auto-generated method
                                                // stub
                                                if (isLike) {
                                                    reqisLike(id, false);
                                                } else {
                                                    reqisLike(id, true);
                                                }

                                            }
                                        });
                                mTextLikeNum.setText(num);

                            } catch (Exception e) {
                                // TODO: handle exception
                            }

                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }



}
